﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class shop_Article_Edit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetPageNoCache();

        if (!this.IsPostBack)
        {
            ReadAction();
            BindData();
        }
    }

    protected void btnCancle_Click(object sender, EventArgs e)
    {
        string sType = hType.Value;

        Response.Redirect("Article_Manager.aspx?Type=" + sType);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtTitle.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "标题不能为空！");

            return;
        }

        if (tbContent.Value.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "内容不能为空！");

            return;
        }

        int ArticleTypeID = 0;
        string sType = hType.Value;

        //Type:1 商城公告 2 购物指南 3付款方式 4 帮助中心 5常见问题
        if (sType == "SCGG")
        {
            ArticleTypeID = 1;
        }
        else if (sType == "GWZN")
        {
            ArticleTypeID = 2;
        }
        else if (sType == "FKFS")
        {
            ArticleTypeID = 3;
        }
        else if (sType == "BZZX")
        {
            ArticleTypeID = 4;
        }
        else if (sType == "CJWT")
        {
            ArticleTypeID = 5;
        }

        DAL.Tables.tbl_Site_Article Article = new DAL.Tables.tbl_Site_Article();

        Article.Title.Value = txtTitle.Text.Trim();
        Article.Content.Value = tbContent.Value.Trim();
        Article.isShow.Value = (radYes.Checked == true) ? true : false;
        Article.ArticleTypeID.Value = ArticleTypeID;
        Article.ShopID.Value = _Site.ID;

        string Action = hAction.Value;

        if (Action == "Add")
        {
            long i = Article.Insert();

            if (i >= 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "保存成功！", "Article_Manager.aspx?Type=" + sType);
            }
            else
            {
                Shove._Web.JavaScript.Alert(this.Page, "保存失败！");
            }
        }
        else if (Action == "Edit")
        {
           long ID = Shove._Convert.StrToLong(hID.Value, -1);
           long i = Article.Update("ID=" + ID.ToString());

           if (i >= 0)
           {
               Shove._Web.JavaScript.Alert(this.Page, "保存成功！", "Article_Manager.aspx?Type=" + sType);
           }
           else
           {
               Shove._Web.JavaScript.Alert(this.Page, "保存失败！");
           }
        }
    }

    /// </summary>
    /// 设置页面不被缓存
    /// </summary>
    public void SetPageNoCache()
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.AddHeader("pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("cache-control", "private");
        HttpContext.Current.Response.CacheControl = "no-cache";
    }

    private void ReadAction()
    {
        string sAction = Shove._Web.Utility.GetRequest("Action");
        string sType = Shove._Web.Utility.GetRequest("Type");
        string sID = Shove._Web.Utility.GetRequest("ID");

        this.hAction.Value = sAction;
        this.hType.Value = sType;
        this.hID.Value = sID;

        //Type:1 商城公告 2 购物指南 3付款方式 4 帮助中心 5常见问题
        if (sType == "SCGG")
        {
            lblInfo.Text = "商城公告";
        }
        else if (sType == "GWZN")
        {
            lblInfo.Text = "购物指南";
        }
        else if (sType == "FKFS")
        {
            lblInfo.Text = "付款方式";
        }
        else if (sType == "BZZX")
        {
            lblInfo.Text = "帮助中心";
        }
        else if (sType == "CJWT")
        {
            lblInfo.Text = "常见问题";
        }

    }

    private void BindData()
    {
        if (hAction.Value != "Edit")
        {
            return;
        }

        long ID = Shove._Convert.StrToLong(hID.Value, -1);

        DAL.Tables.tbl_Site_Article Article = new DAL.Tables.tbl_Site_Article();
        DataTable dtArticle = Article.Open("", "ID=" + ID.ToString(), "");

        txtTitle.Text = dtArticle.Rows[0]["Title"].ToString();
        tbContent.Value = dtArticle.Rows[0]["Content"].ToString();

        if (Shove._Convert.StrToBool(dtArticle.Rows[0]["isShow"].ToString(), false))
        {
            radYes.Checked = true;
        }
        else
        {
            radYes.Checked = false;
        }
    }
}
